﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Entities;
using System.Data.SqlClient;
namespace DAL
{
    public class DAL_Request
    {
        DBConnection db = new DBConnection();
        SqlCommand cmd;
        En_Request er = new En_Request();
        En_Users eu = new En_Users();
        En_UserType eut = new En_UserType();

        DataTable dt;
        DataSet ds;

        public void addRequest(string RequestTypeID, string UserID, string RequestTitle, string RequestContent, string RequestPriority, DateTime RequestDate, string UserAssignee, string RequestStatus, string Remark)
        {
            En_Request er = new En_Request();
            SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=DB_OnlineHelpDesk;Persist Security Info=True;User ID=sa;Password=123456");
            con.Open();
            cmd = new SqlCommand("sp_InsertRequests", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@RequestTypeID", SqlDbType.NVarChar).Value = RequestTypeID;
            cmd.Parameters.Add("@UserID", SqlDbType.NVarChar).Value = UserID;
            cmd.Parameters.Add("@RequestTitle", SqlDbType.NVarChar).Value = RequestTitle;
            cmd.Parameters.Add("@RequestContent", SqlDbType.NVarChar).Value = RequestContent;
            cmd.Parameters.Add("@RequestPriority", SqlDbType.NVarChar).Value = RequestPriority;
            cmd.Parameters.Add("@RequestDate", SqlDbType.SmallDateTime).Value = RequestDate;
            cmd.Parameters.Add("@UserAssignee", SqlDbType.NVarChar).Value = UserAssignee;
            cmd.Parameters.Add("@RequestStatus", SqlDbType.NVarChar).Value = RequestStatus;
            cmd.Parameters.Add("@Remark", SqlDbType.NVarChar).Value = Remark;
            cmd.ExecuteNonQuery();
            db.closeConnection();
        }

        public string getUserbyID(string UserName)
        {
            En_Request user = new En_Request();
            DBConnection db = new DBConnection();
            db.openConnection();
            String UserID = "";
            SqlParameter[] arrPar = { new SqlParameter("@UserName", eu.UserName) };
            arrPar[0].Value = UserName;
            dt = db.getDataTable("sp_GetUserIDbyUserName", arrPar);
            foreach (DataRow dr in dt.Rows)
            {
                UserID = dr["UserID"].ToString();
            }
            db.closeConnection();
            return UserID;
        }


        public string getTypeNamebyTypeID(int TypeID)
        {
            En_RequestType user = new En_RequestType();
            DBConnection db = new DBConnection();
            db.openConnection();
            String TypeName = "";
            SqlParameter[] arrPar = { new SqlParameter("@TypeID", eut.TypeID) };
            arrPar[0].Value = TypeID;
            dt = db.getDataTable("sp_GetTypeNamebyTypeID", arrPar);
            foreach (DataRow dr in dt.Rows)
            {
                TypeName = dr["TypeName"].ToString();
            }
            db.closeConnection();
            return TypeName;
        }

        public void UpDateProfile(String UserName, String UserFullName, int UserAge, String UserGender, String UserEmail)
        {
            En_Users eu = new En_Users();
            SqlParameter[] arrPar = { new SqlParameter("@UserName", eu.UserName), new SqlParameter("@userFullName", eu.UserFullName), new SqlParameter("@userAge", eu.UserAge), new SqlParameter("@UserGender", eu.UserGender), new SqlParameter("@UserEmail", eu.UserEmail) };
            arrPar[0].Value = UserName;
            arrPar[1].Value = UserFullName;
            arrPar[2].Value = UserAge;
            arrPar[3].Value = UserGender;
            arrPar[4].Value = UserEmail;
            db.UpdateData("sp_updateUserByName", arrPar);
        }


        public void UpdatePassword(String UserName, String UserPassword)
        {
            En_Users eu = new En_Users();
            SqlParameter[] arrPar = { new SqlParameter("@UserName", eu.UserName), new SqlParameter("@UserPassword", eu.UserPassword) };
            arrPar[0].Value = UserName;
            arrPar[1].Value = UserPassword;
            db.UpdateData("sp_updatePassword", arrPar);
        }

        

       
    }
}